import { createRouter, createWebHistory } from 'vue-router';
import Home from '../views/Home.vue';
import Login from '../views/Login.vue';
import FileUpload from '../views/FileUpload.vue';
import DataTable from '../views/DataTable.vue';
import { useAuthStore } from '../store/auth';

const routes = [
    {
        path: '/',
        redirect: '/login',
    },
    {
        path: '/login',
        component: Login,
    },
    {
        path: '/home',
        component: Home,
        meta: { requiresAuth: true },
        children: [
            {
                path: 'upload',
                component: FileUpload,
            },
            {
                path: 'data-table',
                component: DataTable,
            },
        ],
    },
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    const authStore = useAuthStore();
    if (to.meta.requiresAuth && !authStore.isLoggedIn) {
        next('/login');
    } else {
        next();
    }
});

export default router;